package com.ita.osc.listener;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.ita.osc.dao.DishDao;
import com.ita.osc.daoImpl.DishDaoImpl;

/**
 * Application Lifecycle Listener implementation class ContextListener
 *
 */
public class ContextListener implements ServletContextListener {

	DishDao dishDao = new DishDaoImpl();

	/**
	 * Default constructor.
	 */
	public ContextListener() {
		// TODO Auto-generated constructor stub
//		System.out.println("2");
	}

	/**
	 * @see ServletContextListener#contextDestroyed(ServletContextEvent)
	 */
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
//		System.out.println("3");
	}

	/**
	 * @see ServletContextListener#contextInitialized(ServletContextEvent)
	 */
	public void contextInitialized(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
		ServletContext context = arg0.getServletContext();
		ScheduledExecutorService pool2 = Executors.newScheduledThreadPool(1);
		pool2.scheduleAtFixedRate(new Runnable() {
			@Override
			public void run() {
				System.out.println("************************* update dish list! *************************");
				context.setAttribute("dishList", dishDao.listAllDish());
			}
		}, 0, 30, TimeUnit.MINUTES);
	}

}
